import { 
    CHANGE_TOP_BANNERS,
    CHANGE_HOT_RECOMMEND,
    CHANGE_NEW_ALBUM,
    CHANGE_UP_RANKING,
    CHANGE_NEW_RANKING,
    CHANGE_ORIGIN_RANKING,
    CHANGE_SETTLE_SINGER
 } from "./constants";

//  import services from "@/services/request"
import { 
    getTopBanners,
    getHotRecommends,
    getNewAlbums,
    getRanking,
    getArtistList
 } from "@/services/recommend";


// 轮播图action对象
const changeTopBannerAction = (res) => {
    return {
        type: CHANGE_TOP_BANNERS,
        topBanners: res.banners
    }
}

const changeHotRecommendAction = (res) => {
    return {
        type: CHANGE_HOT_RECOMMEND,
        hotRecommends: res.result
    }
}

const changeNewAlbumAction = (res) => {
    return {
        type: CHANGE_NEW_ALBUM,
        newAlbums: res.albums
    }
}

const changeRecommendRankingAction = (idx, res) => {
    switch (idx) {
        case 0:
            return {
                type: CHANGE_UP_RANKING,
                playlist: res.playlist
            }
        case 2:
            return {
                type: CHANGE_NEW_RANKING,
                playlist: res.playlist
            }
        case 3:
            return {
                type: CHANGE_ORIGIN_RANKING,
                playlist: res.playlist
            }
        default:
            return {}
    }
    
}

const changeSettleSingAction = (res) => {
    return {
        type: CHANGE_SETTLE_SINGER,
        settleSings: res.artists
    }
}


// getTopBannerAction 函数 可能会传递一些参数 比如  page, limt
export const getTopBannerAction = () => {
    return (dispatch, getState) => {
        getTopBanners().then(res => {
            dispatch(changeTopBannerAction(res))
        })
    }
}

export const getHotRecommendAction = (limit) => {
    return (dispatch) => {
        getHotRecommends(limit).then(res => {
            // console.log(res);
            dispatch(changeHotRecommendAction(res));
            
        })
    }
}

export const getNewAlbumAction = (limit) => {
    return dispatch => {
        getNewAlbums(limit).then(res => {
            dispatch(changeNewAlbumAction(res))
        })
    }
}


export const getRecommendRankingAction = (idx) => {
    return dispatch => {
        getRanking(idx).then(res => {
            dispatch(changeRecommendRankingAction(idx, res))
        })
    }
}

//入住歌手数据
export const getSettleSingersAction = (cat, limit) => {
    return dispatch => {
        getArtistList(cat, limit).then(res => {
            dispatch(changeSettleSingAction(res))
        })
    }
}
